

import 'package:flutter/material.dart';
import 'package:flutter_demos/demos/expansion_panel_demo/expansion_panel_item.dart';

class ExpansionPanelDemo extends StatefulWidget {
  @override
  _ExpansionPanelDemoState createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {

   List<ExpansionPanelItem> _data = generateItems(8);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        child: _buildPanel(),
      ),
    );
  }

  Widget _buildPanel(){
    return ExpansionPanelList(
       expansionCallback: (int index,bool isExpanded){
          setState(() {
            _data[index].isExpanded = !isExpanded;
          });
       },
      children: _data.map<ExpansionPanel>((ExpansionPanelItem item){
          return ExpansionPanel(
            headerBuilder: (BuildContext context,bool isExpanded){
              return ListTile(
                title: Text(item.headerValue + "aaaaa"),
              );
            },
            body: ListTile(
              title:Text(item.expandedValue + "bbbb") ,
              subtitle: const Text('To delete this panel, tap the trash can icon'),
              trailing: const Icon(Icons.delete),
              onTap: (){
                setState(() {
                  _data.removeWhere((ExpansionPanelItem currentItem) => item == currentItem);
                });
              },
            ),
            isExpanded: item.isExpanded
          );
      }).toList(),
    );
  }
}

List<ExpansionPanelItem> generateItems(int numberOfItems){
  return List<ExpansionPanelItem>.generate(numberOfItems, (index) {
    return ExpansionPanelItem(headerValue: 'This is item numbe $index',
                              expandedValue: 'Panel $index '
        );

  });
}
